import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;

Page({
  ...HttpUtils.func,
  data: {
    voteId: '',
    totalCount: 0,
    status: '',
    title: '',
    partyVoteResult:[],
  },
  onLoad(params) {
    var voteId = params.voteId;
    this.setData({
      voteId: voteId
    });
    //投票结果查询接口
    this.loadpartyVoteResult();
  },

  //投票结果查询接口
  loadpartyVoteResult: function(e) {
    var newUrl = getApp().globalData.dangjianUrl + "/vote/partyVoteResult";
 		var params = {};
		params["voteId"] = this.data.voteId + ""; 
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "投票结果查询接口调用失败").then(
      (respData) => {
        console.log(respData)
        var result = respData.data.result.code;
        if (1 == result) {
          var votes = respData.data.vote;
          var totalCount = votes.totalCount;
          var title = votes.title;
          votes = votes.options;
          var partyVoteResult = [];
          for (var index in votes) {
            var vote = votes[index];
            var json = {};
            json["count"] = vote.count;
            json["optionName"] = vote.optionName;
            json["percentage"] = Math.round(vote.count/totalCount*100);
            partyVoteResult.push(json);
          }
          console.log(partyVoteResult)
          this.setData({
            "partyVoteResult": partyVoteResult,
            "totalCount": totalCount,
            "title": title, 
          });
        } else {
          this.showPageMsg("alert", respData.data.result.description) 
        }
      }
    );
  },

  onReachBottom() {
    // 页面被拉到底部    
    // if (this.data.pageIndex > this.data.totalPages) {
    //   this.showPageMsg("tips","没有更多数据了!");
    // } else {
    //   this.loadMsg();
    // }
  },
  onPullDownRefresh: function() {
    // this.setData({
    //   pageIndex:1,
    //   msgList:[]
    // });
    // this.loadMsg();
    // dd.stopPullDownRefresh();
  }
});
